package com.example.demo_homework2.service;

import com.example.demo_homework2.bean.CmsMember;
import com.example.demo_homework2.mapper.CmsMemberMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class CmsMemberService {
    @Autowired
    CmsMemberMapper memberMapper;

    public List<CmsMember> getAllMeber() {
        return memberMapper.selectAll();
    }

    public void saveOrUpdate(CmsMember cmsMember) {
        if (cmsMember.getId() != null) {
            memberMapper.updateByPrimaryKey(cmsMember);
        } else {
            cmsMember.setId(UUID.randomUUID().toString());
            memberMapper.insert(cmsMember);
        }
    }

    public int deleteByid(String id) {
        return memberMapper.deleteByPrimaryKey(id);
    }

    public String deleteByids(String[] ids) {
        ArrayList<String> suc = new ArrayList<String>();
        ArrayList<String> noExist = new ArrayList<String>();
        if (ids != null && ids.length > 0) {
            for (String id : ids) {
                int i = deleteByid(id);
                if (i == 0) {
                    noExist.add(id);
                }
                if (i == 1) {
                    suc.add(id);
                }
            }
            String str = "success:";
            if (suc.size() > 0) {
                str += "成功删除了" + suc + "!";
            }
            if (noExist.size() > 0) {
                str += "会员" + noExist + "不存在";
            }
            return str;
        } else {
            return "请输入要删除的会员编号";
        }
    }

}
